<template>
  <section v-loading="loading" class="cms-body">
    <div class="cms-list-header">
      <el-button type="primary" icon="el-icon-plus" @click="routerLink('/sysUser/save','save',0)"
                 v-perms="'/sysUser/save'">添加
      </el-button>
      <cms-input v-model="params.queryUsername" label="用户名"></cms-input>
      <cms-input v-model="params.queryRealName" label="作者"></cms-input>
      <cms-input v-model="params.queryEmail" label="电子邮箱"></cms-input>
      <el-checkbox v-model="params.queryStatu" true-label="1" false-label="">禁用</el-checkbox>
    </div>
    <div class="cms-list-header">
      <el-select v-model="params.queryDepartId" @change="query" style="width: auto;" clearable>
        <el-option label="所有部门" value=""></el-option>
        <el-option v-for="(item,index) in departments" :key="index" :label="item.name" :value="item.id"></el-option>
      </el-select>
      <el-select v-model="params.queryRoleId" @change="query" style="width: auto;" clearable>
        <el-option label="所有角色" value=""></el-option>
        <el-option v-for="(item,index) in roles" :key="index" :label="item.name" :value="item.id"></el-option>
      </el-select>
      <el-button @click="query">查询</el-button>
    </div>
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="checkIds">
      <el-table-column type="selection" width="35" align="right"></el-table-column>
      <el-table-column prop="id" label="ID" width="50" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" align="center"></el-table-column>
      <el-table-column prop="roleNames" label="角色" align="center">
        <template slot-scope="scope">
          {{scope.row.roleNames.join(' | ')}}
        </template>
      </el-table-column>
      <el-table-column prop="realname" label="作者" align="center"></el-table-column>
      <el-table-column prop="departName" label="部门" align="center">
        <template slot-scope="scope">
          {{scope.row.departmentNames.toString()}}
        </template>
      </el-table-column>
      <el-table-column prop="lastLoginTime" label="最后登录时间" align="center"></el-table-column>
      <el-table-column prop="disabled" label="禁用" align="center">
        <div slot-scope="scope">
          <span v-if="scope.row.disabled">是</span>
          <span v-else>否</span>
        </div>
      </el-table-column>
      <el-table-column prop="loginCount" label="登录次数" align="center"></el-table-column>
      <el-table-column prop="name" label="操作" align="center" width="80">
        <div slot-scope="scope">
          <cms-button type="edit"
                      @click.native="routerLink('/sysUser/edit',scope.row.rank,scope.row.id)"
                      v-perms="'/sysUser/edit'"></cms-button>
          <cms-button type="delete" @click.native="deleteBatch($api.adminGlobleDelete,scope.row.id)"
                      v-perms="'/sysUser/delete'" v-if="scope.row.username!='admin'"></cms-button>
        </div>
      </el-table-column>
    </el-table>
    <!-- 表格底部 -->
    <div class="cms-list-footer">
      <div class="cms-left">
      </div>
      <!-- cms分页组件 -->
      <cms-pagination :total="total" @change="getPages"></cms-pagination>
    </div>
  </section>
</template>
<script>
  import listMixins from '@/mixins/list'
  import axios from "axios";

  export default {
    mixins: [listMixins],
    data() {
      return {
        params: {//只需要业务参数
          queryUsername: "",
          queryEmail: "",
          queryGroupId: "",
          queryStatu: "",
          queryRealName: "",
          queryDepartId: "",
          queryRoleId: "",
          pageNo: "",
          pageSize: "",
          https: ""
        },
        roles: [],
        departments: [],
      };
    },
    methods: {
      getQueryParams() {
        let api = this.$api; //API地址
        axios.all([
          axios.post(api.roleList), //axios批量发送请求
          axios.post(api.departmentList,{all:false}),
        ]).then(
          axios.spread((roles, departments) => {
            this.roles = roles.body;
            this.departments = departments.body;
            this.loading = false;
          })
        ).catch(err => {
          this.loading = false;
        });
      }
    },
    created() {
      if (this.$route.query.id != '0') {
        let userType = this.$route.query.userType;
        if (!this.$myUtils.isNull(userType)) {
          if (userType == 1) {
            this.params.userId = this.$route.query.id;
          }
          if (userType == 2) {
            this.params.checkUserId = this.$route.query.id;
          }
        }
      }
      this.initTableData(this.$api.adminGlobleList, this.params);
      this.getQueryParams();
    }
  };
</script>

<style>

</style>
